package com.zhanghe.mall.service.impl;

import com.zhanghe.mall.pojo.Manager;
import com.zhanghe.mall.pojo.ManagerRole;
import com.zhanghe.mall.pojo.Permissions;
import com.zhanghe.mall.pojo.RolePermissions;
import com.zhanghe.mall.pojo.repository.ManagerRepository;
import com.zhanghe.mall.pojo.repository.ManagerRoleRepository;
import com.zhanghe.mall.pojo.repository.PermissionsRepository;
import com.zhanghe.mall.pojo.repository.RolePermissionsRepository;
import com.zhanghe.mall.service.ManagerService;
import com.zhanghe.mall.service.vo.ManagerInfoVO;
import com.zhanghe.mall.service.vo.ManagerPermissionsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Throwable.class)
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private ManagerRepository managerRepository;
    @Autowired
    private ManagerRoleRepository managerRoleRepository;
    @Autowired
    private RolePermissionsRepository rolePermissionsRepository;
    @Autowired
    private PermissionsRepository permissionsRepository;

    @Override
    public Manager findByGithubAuthid(Long githubAuthid){
        return managerRepository.findByGithubAuthid(githubAuthid);
    }

    @Override
    public Manager findByUserName(String userName) {
        return managerRepository.findByUserName(userName);
    }
    @Override
    public ManagerInfoVO findManagerInfoVOByUserName(String userName) {
        return ManagerInfoVO.transfrom(managerRepository.findByUserName(userName));
    }

    @Override
    public List<ManagerPermissionsVO> getManagerAllPermissions(Long managerId) {
        List<ManagerPermissionsVO> result = new ArrayList<>();
        List<ManagerRole> roleList = managerRoleRepository.findByManager_Id(managerId);
        List<Long> roleIds = new ArrayList<>();
        for(ManagerRole managerRole:roleList){
            roleIds.add(managerRole.getRole().getId());
        }
        List<RolePermissions> rolePermissionss = rolePermissionsRepository.findByRole_IdInAndPermissions_TypeOrderByPermissionsSortAsc(roleIds,"menu");
        for(RolePermissions rolePermissions:rolePermissionss){
            if(rolePermissions.getPermissions().getParentId()==null){
                ManagerPermissionsVO vo = ManagerPermissionsVO.transformFromRolePermissions(rolePermissions);
                getPermissionsChild(vo ,rolePermissionss);
                result.add(vo);
            }
        }
        return result;
    }

    public void getPermissionsChild(ManagerPermissionsVO vo,List<RolePermissions> rolePermissionss){
        List<ManagerPermissionsVO> child = new ArrayList<>();
        for(RolePermissions rolePermissions:rolePermissionss){
           if(vo.getId().equals(rolePermissions.getPermissions().getParentId())){
               ManagerPermissionsVO childvo = ManagerPermissionsVO.transformFromRolePermissions(rolePermissions);
               getPermissionsChild(childvo ,rolePermissionss);
               child.add(childvo);
           }
        }
        vo.setChild(child);
    }
}
